﻿@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_LayoutIndex.cshtml";
}
@using LeSoft.Application.Code;
<script>
    var _organizeid = "@LeSoft.Application.Code.OperatorProvider.Provider.Current().CompanyId"
    var keyValue = request('keyValue');
    var _State = "", OrganizeId = "", RoomId = "";
    var Color = [];
    var Data = [];
    var WCount = [];

    $(function () { 
        InitialPage();
        InitControl();
        GetGrid();
        setTimeout('GetTree()', 300);
        setTimeout('getScaledata()', 500);
        if (!!keyValue) {
            RoomId = keyValue;
            GetGrid();
            GetScaleByPSId(RoomId);
            GetAllName(RoomId);
        }
    })

    function InitControl() {
        $.SetForm({
            url: "../../ResourceManage/PStructColor/GerColorList",
            success: function (data) {
                if (data.length <= 0) {
                    GetDefaultColor();
                    return false;
                }
                Color = data;
                var html = "";
                for (var i = 0; i < data.length; i++) {

                    $("#" + data[i].Id).css("backgroundColor", data[i].Color);
                    html += "<div style='float: left; background-color: " + data[i].Color + "; width: 70px; height: 20px; line-height: 20px; text-align: center; margin-left: 20px; '>" + data[i].StateName + "</div>"

                }
                $("#Color").html(html);
            }
        })
    };
    function GetTree() {
        var item = {
            height: $(window).height() - 370,
            url: "../../ResourceManage/RS_PStructs/GetTreeJson?IsSubstep=true&type=2&OrganizeId=" + OrganizeId,
            onnodeclick: function (item) {
                RoomId = item.id;
                GetAllName(RoomId);
                Data = [];
                GetGrid();
                GetScaleByPSId(RoomId);
            }
        }
        $("#itemTree1").treeview(item);
    };
    function GetScaleByPSId(RoomId) {
        $("#Tab").html(""); 
        $.ajax({
            url: "../../ResourceManage/PStructColor/GetScaleByPSId?keyValue=" + RoomId,
            type: "post",
            dataType: "json",
            success: function (data) {
                $("#Tab").html(data);
            }
        }) 
      
    }
    function InitialPage() {
        $('#layout').layout({
            applyDemoStyles: true,
            onresize: function () {
                $(window).resize()
            }
        });
        //$(window).resize(function (e) {
        //    window.setTimeout(function () {
        //        $('#gridTable').setGridWidth(($('.gridPanel').width()));
        //        $("#gridTable").setGridHeight($(window).height() - 181.5);

        //    }, 200);
        //    e.stopPropagation();

        //});
        $("#OrganizeId").ComboBoxTree({
            url: "../../BaseManage/Organize/GetTreeJsonById",
            height: "250px",
            description: "===选择组织机构===",
            click: function (item) {
                OrganizeId = item.id;
                GetTree();
            }
        });
        $("#OrganizeId").ComboBoxSetValue(_organizeid);

    };

    function getScaledata() {
        //初始化统计表
        $.ajax({
            url: "/ResourceManage/PStructColor/GetScale",
            type: "post",
            dataType: "json",
            success: function (data) { 
                $("#Tab").html(data);
            }
        })
    }
    function GetGrid() {
        if (RoomId == "") {
            return false;
        }
        $gridTable = $("#gridTable");
        $gridTable.html("");
        var html = "";
        var result = "";
        var a = 0, b = 1;
        $.ajax({
            url: "/ResourceManage/PStructColor/GetData?keyValue=" + RoomId,
            type: "get",
            dataType: "json",
            success: function (data) { 
                if (data.Entity != null) {
                    if (data.Entity.Type == 1) {//楼盘-楼阁-楼层-房间
                        b += parseInt(data.Count);
                        Data.push({ "Type": 0, "Name": "", "FJ": null });
                        var arr2 = $.grep(data.Lst2, function (value) {
                            return value.ParentId == data.Entity.Id;
                        })//楼阁
                        if (arr2.length > 0) {
                            for (var s = 0; s < arr2.length; s++) {
                                var arr2_1 = $.grep(data.Lst2, function (value) {
                                    return value.ParentId == arr2[s].Id;
                                })//子楼阁
                                if (arr2_1.length > 0) {
                                    arr2.push(arr2_1);
                                }
                            }
                            for (var i = 0; i < arr2.length; i++) {
                                var arr4 = $.grep(data.Lst4, function (value) {
                                    return value.ParentId == arr2[i].Id;
                                }) //楼层
                                if (arr4.length > 0) {
                                    Data.push({ "Type": 1, "Name": arr2[i].Name });
                                    b += arr4.length;
                                    for (var j = 0; j < arr4.length; j++) {
                                        a = arr4[j].JuniorNum > a ? arr4[j].JuniorNum : a;
                                        var arr5 = $.grep(data.Lst5, function (value) {
                                            return value.ParentId == arr4[j].Id;
                                        }) //房间
                                        Data.push({ "Type": 2, "Name": arr4[j].Name, "FJ": arr5 });
                                        a = arr5.length > a ? arr5.length : a;
                                        if (arr5.length>0) {
                                            for (var f = 0; f < arr5.length; f++) {
                                                var arr6 = $.grep(data.Lst5, function (value) {
                                                    return value.ParentId == arr5[f].Id;
                                                })
                                                if (arr6.length>0 && arr5[f].JuniorNum-1==f) {
                                                    WCount[f] = WCount[f] >= arr6.length ? WCount[f] : arr6.length;
                                                }
                                            }
                                            
                                        }
                                    }
                                }
                                else {
                                    var arr5 = $.grep(data.Lst5, function (value) {
                                        return value.ParentId == arr2[i].Id;
                                    }) //房间
                                    Data.push({ "Type": 2, "Name": arr2[i].Name, "FJ": arr5 });
                                     
                                }

                            }
                        }
                        else {//楼盘-楼层-房间
                            var arr4 = $.grep(data.Lst4, function (value) {
                                return value.ParentId == data.Entity.Id;
                            }) //楼层
                            if (arr4.length > 0) {
                                b += arr4.length;
                                for (var j = 0; j < arr4.length; j++) {
                                    a = arr4[j].JuniorNum > a ? arr4[j].JuniorNum : a;
                                    var arr5 = $.grep(data.Lst5, function (value) {
                                        return value.ParentId == arr4[j].Id;
                                    }) //房间
                                    Data.push({ "Type": 2, "Name": arr4[j].Name, "FJ": arr5 });
                                }
                            }
                            else { //楼盘-房间
                               
                                var arr5 = $.grep(data.Lst5, function (value) {
                                    return value.ParentId == data.Entity.Id;
                                }) //房间
                                b += 1;
                                Data.push({ "Type": 2, "Name": data.Entity.Name, "FJ": arr5 });
                                a = arr5.length > a ? arr5.length : a;
                                
                            }

                        }
                    }
                    if (data.Entity.Type == 2) {//楼阁

                        Data.push({ "Type": 0, "Name": "", "FJ": null });
                        var arr4 = $.grep(data.Lst4, function (value) {
                            return value.ParentId == data.Entity.Id;
                        }) //楼层
                        if (arr4.length > 0) {
                            b += data.Entity.JuniorNum;
                            for (var j = 0; j < arr4.length; j++) {
                                a = arr4[j].JuniorNum > a ? arr4[j].JuniorNum : a;
                                var arr5 = $.grep(data.Lst5, function (value) {
                                    return value.ParentId == arr4[j].Id;
                                }) //房间
                                Data.push({ "Type": 2, "Name": arr4[j].Name, "FJ": arr5 });
                            }
                        } else {
                            var arr5 = $.grep(data.Lst5, function (value) {
                                return value.ParentId == data.Entity.Id;
                            }) //房间
                            b++;
                            Data.push({ "Type": 2, "Name": data.Entity.Name, "FJ": arr5 });
                            a = arr5.length > a ? arr5.length : a;
                            
                        }

                    }
                    
                    html = "<table>";
                    for (var i = 0; i < b; i++) {
                        if (i == 0) {
                            html += "<tr class='cs1'><th></th>"
                        }
                        else {
                            if (Data[i].Type == 2) {
                                html += "<tr class='cs1'><td class='cs2'>" + Data[i].Name + "</td>"
                            }
                            else {
                                html += "<tr class='cs1'><td class='cs2' style=' text-align: left;background-color: #fff; ' colspan='" + a + 1 + "'>" + Data[i].Name + "</td>"
                            }
                        }
                        var s = 0;
                        if (Data[i].Type == 2) {
                            var arr = Data[i].FJ;
                            for (var j = 0; j < a; j++) {
                                
                                var arr1 = $.grep(arr, function (value) {
                                    return value.JuniorNum == j + 1;
                                });
                                if (arr1.length > 0) {
                                    
                                    var Id = arr1[0].Name;
                                    var arr6 = $.grep(data.Lst5, function (value) {
                                        return value.ParentId == arr1[0].Id;
                                    }) //床位
                                    if (arr6.length <= 0) {
                                        html += "<td class='cs3'style='background-color:" + GetColor(arr1[0].IsPublish) + "'  onclick='btn_Details(\"" + arr1[0].Id + "\")' >" + arr1[0].Name + "</td>"
                                    }
                                    else {
                                        debugger;
                                        var W = WCount[j];
                                        var width = parseFloat(parseFloat(1.00 / parseFloat(arr6.length)) * 100);
                                        html += "<td class='CS_CWDV' style=' width:" + 100 * (W) + "px; '>" 
                                        for (var k = 0; k < arr6.length; k++) {
                                            html += "<div class='CS_CW' style='float:left; width:"+width+"%; background-color:" + GetColor(arr6[k].IsPublish) + "'  onclick='btn_Details(\"" + arr6[k].Id + "\")'>" + arr6[k].Name + "</div>";
                                        }
                                        html += "</td>"
                                    }
                                }
                                else {
                                    html += "<td class='cs4'></td>"
                                }
                            }

                        } else {
                            for (var j = 0; j < a; j++) {
                                if (i == 0) {
                                    html += "<th class='cs4'>" + parseInt(j + 1) + "</th>"
                                }

                            }
                        }

                        if (i == 0) {
                            html += "</tr>"
                        }
                    }
                    html += "</table>";
                    $gridTable.html(html); 
                }


            }
        })
    };
    function btn_Details(keyValue) {
        if (checkedRow(keyValue)) {
            dialogOpen({
                id: "PSForm",
                title: '房间详情',
                btn: null,
                url: '/ResourceManage/PStructColor/PSForm?keyValue=' + keyValue,
                width: "1100px",
                height: "750px",
                callBack: function (iframeId) {
                    top.frames[iframeId].AcceptClick();
                }
            })
        }
    }

    //颜色配置
    function btn_cog() { 
        dialogOpen({
            id: "Form",
            title: '颜色配置(点击色块可更换颜色)',
            url: '/ResourceManage/PStructColor/Form ',
            width: "700px",
            height: "500px",
            callBack: function (iframeId) {
                top.frames[iframeId].AcceptClick();
            }
        });
    };
    function GetColor(s) {
        var arr = $.grep(Color, function (value) {
            return value.State == s;
        })
        if (arr.length==0) {
            return "#fff"; 
        }
        return arr[0].Color;
    }
    function GetDefaultColor() {
        $.SetForm({
            url: "../../ResourceManage/PStructColor/GetDefaultColor",
            success: function (data) {
                Color = data;
                var html = "";
                for (var i = 0; i < data.length; i++) {

                    $("#" + data[i].Id).css("backgroundColor", data[i].Color);
                    html += "<div style='float: left; background-color: " + data[i].Color + "; width: 70px; height: 20px; line-height: 20px; text-align: center; margin-left: 20px; '>" + data[i].StateName + "</div>"

                }
                $("#Color").html(html);
            }
        })
    }
    function GetAllName(s) {
        $.ajax({
            url: "/ResourceManage/PStructColor/GetAllName?keyValue=" + s,
            type: "post",
            dataType: "json",
            success: function (data) {
                $("#MM").html(data);
            }
        })
    }
     
    function SetWCount()
    {
        for (var i = 0; i < 50; i++) {
            WCount.push(1);
        }
    }
</script>
<style>
    #Tab {
        width: 100%;
    }

        #Tab table {
            width: 95%;
            margin: 10px auto;
            border: solid 1px #ccc;
            text-align: center;
        }

            #Tab table td, #Tab table th {
                border: solid 1px #ccc;
                line-height: 30px;
                text-align: center;
            }

    #gridTable {
        background-color: #EDE8E8;
    }

        #gridTable table {
            margin: 10px auto;
            border: solid 1px #ccc;
            text-align: center;
        }

            #gridTable table td, #gridTable table th {
                width: 100px;
            }

    .cs1 {
        height: 30px;
        width: 100px;
    }

    .cs2 {
        height: 20px;
        line-height: 20px;
        border: solid 1px #ccc;
        text-align: center;
        background-color: #EDE8E8;
        width: 100px;
    }

    .cs3 {
        height: 20px;
        line-height: 20px;
        border: solid 1px #ccc;
        text-align: center;
        width: 100px;
        cursor: pointer;
    }

        .cs3:hover {
            border: none;
            -webkit-box-shadow: 0 1px 40px rgba(0, 0, 0, 0.27), 0 0 60px rgba(0, 0, 0, 0.06) inset;
            position: relative;
            *zoom: 1;
        }

    .cs4 {
        background-color: #EDE8E8;
        border: solid 1px #ccc;
        width: 100px;
    }

    .CS_CWDV {
        background-color: #EDE8E8;
    }

    .CS_CW {
        height: 32px;
        line-height: 32px;
        border: solid 1px #ccc;
        text-align: center;
         
        cursor: pointer;
        display: inline-block;
        *display: inline;
        *zoom: 1;
    }

        .CS_CW:hover {
            border: none;
            -webkit-box-shadow: 0 1px 40px rgba(0, 0, 0, 0.27), 0 0 60px rgba(0, 0, 0, 0.06) inset;
            position: relative;
            *zoom: 1;
        }
</style>
<div class="ui-layout" id="layout" style="height: 100%; width: 100%;">
    <div class="ui-layout-west">
        <div class="west-Panel">
            <div class="panel-Title" style="padding:0px;"><div id="OrganizeId" type=" selecttree" class="ui-select "></div></div>
            <div id="itemTree1"></div>
            <div class="panel-Title" style="text-align:center;">房态统计</div>
            <div id="Scale" style="height: 270px; width: 100%; border: 1px solid #ccc; ">
                <div id="Tab"></div>
            </div>
        </div>

    </div>
    <div class="ui-layout-center">
        <div class="center-Panel">
            <div class="titlePanel">
                <div class="title-search ">
                    <table>
                        <tr>
                            <td>已选择：</td>
                            <td style="padding-left: 10px; " id="MM"></td>
                            <td style="padding-left: 10px;" id="Color"></td>
                        </tr>
                    </table>

                </div>

                <div class="toolbar">
                    <div class="btn-group">
                        <a id="lr-replace" class="btn btn-default " onclick="reload(); "><i class="fa fa-refresh"></i>&nbsp;刷新</a>
                        <a id="lr-add" class="btn btn-default " onclick="btn_cog()"><i class="fa fa-cog"></i>&nbsp;设置</a>
                    </div>
                    <script>$('.toolbar').authorizeButton()</script>
                </div>
            </div>


            <div class="gridPanel">
                <table id="gridTable"></table>
            </div>

        </div>
    </div>
</div>
